#! /usr/bin/env python
# coding=utf-8
# 读取通过 match_swiss_prot_annotation.py 获得的对应表 将note 添加到gff3 文件



from Bio import SeqIO
import re
import argparse
import sys
from argparse import RawTextHelpFormatter


parser = argparse.ArgumentParser(
    description='''
    读取通过 match_swiss_prot_annotation.py 获得的对应表 将note 添加到gff3 文件
    用法:
    add_note2gff3.py -i Cves_OGS_v3.6.gff3 -s out_gene2swiss_table -o Cves_OGS_v3.7.gff3
    ''',formatter_class=RawTextHelpFormatter)



parser.add_argument('-i',
                help='必须给定，输入的gff3文件 ')

parser.add_argument('-s',
                help='必须给定，match_swiss_prot_annotation.py 输出的 文件')


parser.add_argument('-o',
                help='输出的gff3文件')


# 取出pasa中被 merge的 mRNA

args = parser.parse_args()

if not args.i or not args.o or not args.s:
    parser.print_help()
    sys.exit()

tab_file = args.s

outgff_file = args.o

infile = args.i




# infile = 'Cves_OGS_v3.6.gff3'

# tab_file = r'out_gene2swiss_table'

# outgff_file = r'out.gff3'



outgff_file = open(outgff_file,'w')

#with open()

note_dic = {} # 这里设定 note为 symbol | 描述

with open(tab_file) as fila:
	for i in fila:
		k = i.strip().split('\t')
		if len(k)>1:
			txt = ''
			if k[2]!='':
				txt += k[2]
			if k[3]!='':
				if txt !='':
					txt += '|'
				txt+= k[3]
			note_dic[k[0]] = txt

with open(infile) as fila:
	for i in fila:
		i = i.strip()
		t =  i.split('\t')
		if len(t)>1 and t[2]=='gene':

			#print(t)
			name = t[8].split(';Name=')[1].split(';')[0]
			#print(name)
			if name in note_dic and note_dic[name].strip() !='':
				t[8] = t[8].strip(';')+';Note='+note_dic[name]
				i = '\t'.join(t)
		outgff_file.write(i+'\n')

outgff_file.close()
		